package visualization.components;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;


/**
 * Creates a textbox that will automatically resize its text to fill the size of the textbox
 * @author Ben Schwab
 * 
 */
public class TextBox extends GraphSprite {
    private static final Float FONT_SIZE = 20.0f;
    private String myText;
    private Font myTextBoxFont;

    /**
     * 
     * @param drawArea area of the textbox
     * @param text text to fill textbox with
     */
    public TextBox (Rectangle drawArea, String text) {
        super(drawArea);
        myText = text;

    }

    private void scaleFont (Graphics2D pen) {
        float fontSize = FONT_SIZE;
        Font font = pen.getFont().deriveFont(fontSize);
        int width = pen.getFontMetrics(font).stringWidth(myText);
        fontSize = (float) ((getDrawArea().getWidth() / width) * fontSize);
        font = pen.getFont().deriveFont(fontSize);
        fontSize = (float) ((getDrawArea().getWidth() / width) * fontSize);
        myTextBoxFont = font;

    }
    /**
     * @param pen pen to paint graphics with
     */
    public void paint (Graphics2D pen) {
        scaleFont(pen);
        pen.setFont(myTextBoxFont);
        pen.setColor(Color.BLACK);
        pen.drawString(myText, (int) getDrawArea().getMinX(), (int) getDrawArea().getCenterY());
    }
    /**
     * 
     * @param newText the text to change the textbox to
     */
    public void setText (String newText) {
        myText = newText;
    }

}
